/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.GmnDao;
import gemini.basic.dao.ProductDao;
import gemini.basic.dto.SearchProductCriteria;
import gemini.basic.model.Product;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public Product getByCode(String code) {

		DetachedCriteria dtCri = DetachedCriteria.forClass(Product.class);

		if (code != null && !("".equals(code))) {
			dtCri.add(Restrictions.eq("code", code));
		}

		List<Object> productList = gmnDao.findByCriteria(dtCri);

		Product result = null;
		if (productList != null && !productList.isEmpty()) {
			result = (Product) productList.get(0);
		}
		return result;
	}
	

	@Override
	public List<Product> findProduct(
			SearchProductCriteria criteria) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Product.class);
		dtCri.createAlias("productType","pt");
		
		if(criteria.getCategory()!=null && !criteria.getCategory().isEmpty()){
			dtCri.add(Restrictions.eq("pt.code", criteria.getCategory()));
		}
		
		String nameKey = null;
		
		if (criteria.getSearchKey() != null
				&& !criteria.getSearchKey().isEmpty()) {
			if (!criteria.isByName()) {
				dtCri.add(Restrictions.eq("code", criteria.getSearchKey()));
			} else {
				nameKey = criteria.getSearchKey().toLowerCase();
			}
		}
		
		List<Product> result = null;

		List<Object> disList = gmnDao.findByCriteria(dtCri);
		if (disList != null && !disList.isEmpty()) {
			result = new ArrayList<Product>(disList.size());
			Product pr = null;
			for (Object rsObject : disList) {
				pr = (Product) rsObject;
				if (nameKey == null
						|| pr.getName().toLowerCase().contains(nameKey)) {
					result.add(pr);
				}
			}
		}
		return result;
	}

	@Override
	public List<Product> getAll() {

		DetachedCriteria dtCri = DetachedCriteria.forClass(Product.class);

		List<Object> rsList = gmnDao.findByCriteria(dtCri);
		List<Product> result = null;

		if (rsList != null) {
			result = new ArrayList<Product>(rsList.size());

			for (Object ob : rsList) {
				result.add((Product) ob);
			}
		}

		return result;
	}

	@Override
	public Product getById(int id) {
		Product result = getHibernateTemplate().get(Product.class, id);
		return result;
	}
	
	@Override
	public boolean delete(Product product) {
		getHibernateTemplate().delete(product);
		return true;
	}
	

	@Override
	public Product saveOrUpdate(Product product, boolean flush) {
		Product result = null;
		if (flush) {
			result = (Product) gmnDao.saveOrUpdateAndFlush(product);
		} else {
			result = (Product) gmnDao.saveOrUpdate(product);
		}
		return result;
	}
}
